import { SCORE_RULE } from './config'
interface scoreParams {
    [propname: string]: any
}
export class NewEnergyScore {
    //初始分数
    totalScore = 100
    rule: any[] = []
    init(batteryDetails: any) {
        this.rule.length = 0
        //值
        let propertyValue: any
        //键
        let propertyKey: scoreParams
        for (const key in SCORE_RULE) {
            propertyValue = batteryDetails[key]
            propertyKey = SCORE_RULE[key]
            //检查 值 是否为 number 类型 返回 ture 非数字 false 数字
            if (isNaN(Number(propertyValue))) {
                const result: Array<number | string> = propertyValue.split('-')
                //判断键的对象类型
                if (Object.prototype.toString.call(propertyKey) === '[object Object]') {
                    if ('max' in propertyKey) {
                        //先判断propertyKey属性中是否有 'max' 属性

                        //然后result 与 max 的值进行比较
                        const s = result.some(i => {
                            return Number(i) > propertyKey.max
                        })
                        if (s) {
                            this.totalScore -= propertyKey.deduct
                        }
                        this.rule.push(key, propertyKey, propertyValue, result, '减去', s ? propertyKey.deduct : 0)
                        console.log(key, propertyKey, propertyValue, result, '减去', s ? propertyKey.deduct : 0)
                    } else if ('min' in propertyKey) {
                        //先判断propertyKey属性中是否有 'min' 属性

                        //然后result 与 min 的值进行比较
                        const s = result.some(i => {
                            return Number(i) < propertyKey.min
                        })
                        if (s) {
                            this.totalScore -= propertyKey.deduct
                        }
                        console.log(key, propertyKey, propertyValue, result, '减去', s ? propertyKey.deduct : 0)
                    }
                }
            } else {
                if (Object.prototype.toString.call(propertyKey) === '[object Object]') {
                    if ('max' in propertyKey) {
                        const s = Number(propertyValue) > propertyKey.max ? true : false
                        if (s) {
                            this.totalScore -= propertyKey.deduct
                        }
                        console.log(key, propertyKey, propertyValue, '减去', s ? propertyKey.deduct : 0)
                    } else if ('min' in propertyKey) {
                        const s = Number(propertyValue) < propertyKey.min ? true : false
                        if (s) {
                            this.totalScore -= propertyKey.deduct
                        }
                        console.log(key, propertyKey, propertyValue, '减去', s ? propertyKey.deduct : 0)
                    }
                } else if (Object.prototype.toString.call(propertyKey) === '[object Array]') {
                    let result = 0
                    const s = propertyKey.some((item: any) => {
                        let r = false
                        if ('range' in item) {
                            if (Object.prototype.toString.call(item.range) === '[object Array]') {
                                r =
                                    item.range[0] <= Number(propertyValue) && item.range[1] > Number(propertyValue)
                                        ? true
                                        : false
                                if (r) {
                                    result = item.deduct
                                }
                            } else if (Object.prototype.toString.call(item.range) === '[object Object]') {
                                if ('max' in item.range) {
                                    r = item.range.max < Number(propertyValue) ? true : false
                                    if (r) {
                                        result = item.deduct
                                    }
                                } else if ('min' in item.range) {
                                    r = item.range.min > Number(propertyValue) ? true : false
                                    if (r) {
                                        result = item.deduct
                                    }
                                }
                            }
                        } else {
                            if ('max' in item) {
                                if (!r) {
                                    r = Number(propertyValue) > item.max ? true : false
                                    result = r ? item.deduct : 0
                                }
                            } else if ('min' in item) {
                                if (!r) {
                                    r = Number(propertyValue) < item.min ? true : false
                                    result = r ? item.deduct : 0
                                }
                            }
                        }
                        return r
                    })
                    if (s) {
                        this.totalScore -= result
                    }

                    console.log(key, propertyKey, Number(propertyValue), '减去', result)
                }
            }
        }
        return this.totalScore
    }
}
